import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';
import * as goodsService from '../services/pointService';

export default {
  namespace: 'goods',

  state: {
    goodsList: [],
    pagination: {},

    goodsDetail: {},

    categoryList: [],

    positionList: [],
    scoreGoodsList: [],
  },

  effects: {
    *fetchGoodsList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(goodsService.fetchGoodsList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'goodsListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchGoodsDetail({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.fetchGoodsDetail, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'goodsDetailReducer',
          payload: { data },
        });
        if (callback) { callback(data) }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchCategoryList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(goodsService.fetchCategoryList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'categoryListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addGoods({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.addGoods, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;
        message.success('添加成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *editGoods({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.editGoods, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('修改成功！')
        console.log('添加后返回的数据：', data.goodsDetail);

        if (callback) callback(data.goodsDetail);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *upGoodsList({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(goodsService.upGoodsList, payload)));
      console.log(response)
      if (response.code === 0) {
        message.success('上架成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *downGoodsList({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.downGoodsList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('下架成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *delGoodsList({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.delGoodsList, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('删除成功！')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *addImage({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.addImage, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('添加成功！')

        if (callback) callback(data.showImgUrl);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *fetchPositionList({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(goodsService.fetchPositionList, payload)));
      console.log('fetchPostionList接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;

        yield put({
          type: 'positionListReducer',
          payload: { data },
        });
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchScoreGoodsList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(goodsService.fetchScoreGoodsList, payload)));
      console.log('fetchScoreGoodsList', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'scoreGoodsListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },


    *addPosition({ payload, callback }, { call, put }) {
      console.log('addPosition前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.addPosition, payload)));
      console.log('addPosition接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'positionListReducer',
          payload: { data },
        });
        if (callback)  callback()
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editPosition({ payload, callback }, { call, put }) {
      console.log('editPosition前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(goodsService.editPosition, payload)));
      console.log('editPosition接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'positionListReducer',
          payload: { data },
        });
        if (callback)  callback()
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    goodsListReducer(state, action) {
      return {
        ...state,
        goodsList: action.payload.data.dataList,
        pagination: dealPagination(action.payload.data.pageInfo),
      };
    },
    goodsDetailReducer(state, action) {
      return {
        ...state,
        goodsDetail: action.payload.data,
      };
    },
    categoryListReducer(state, action) {
      return {
        ...state,
        categoryList: action.payload.data,
      };
    },
    positionListReducer(state, action) {
      return {
        ...state,
        positionList: action.payload.data,
      };
    },
    scoreGoodsListReducer(state, action) {
      return {
        ...state,
        scoreGoodsList: action.payload.data,
      };
    },
  },
};
